package com.home766.template.directive;

import com.home766.entity.Scheme;
import com.home766.service.SchemeService;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import net.biz.Filter;
import net.biz.Filter.Operator;
import net.biz.template.directive.BaseDirective;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component("schemeListDirective")
public class SchemeListDirective extends BaseDirective {

	@Autowired
	private SchemeService schemeService;
	
	/** 变量名称 */
	private static final String VARIABLE_NAME = "schemes";
	
	@Override
	@SuppressWarnings("rawtypes")
	public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
		Integer cou = null;
		if(!params.isEmpty()){
			String count = params.get("count").toString();
			if(count != null){
				cou = Integer.parseInt(count);
			}
		}
		List<Filter> filters = new ArrayList<Filter>();
		Filter filter = new Filter();
		//设置查询为已上架的方案
		filter.setProperty("isMarketable");
		filter.setValue(true);
		filter.setOperator(Operator.eq);
		filter.setProperty("isDelete");
		filter.setValue(false);
		filters.add(filter);
		Filter filter1 = new Filter();
		//设置查询为已上架的方案
		filter1.setProperty("isDelete");
		filter1.setValue(true);
		filter1.setOperator(Operator.ne);
		filters.add(filter1);
		List<Scheme> schemes = schemeService.findList(cou, filters, null);
		Map<String, Object> result=new HashMap<String, Object>();
		result.put("schemeLists", schemes);
		setLocalVariable(VARIABLE_NAME, result, env, body);
	}
}
